const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
module.exports = {
  mode: 'development',
  entry: path.resolve(__dirname, './src/js/index.js'),
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: 'js/[name].[contenthash:8].js',
    // assetModuleFilename: 'img/[name].[hash:6][ext]'
  },
  module: {
    rules: [
      {
        enforce: "pre",  //前置loader,最先执行
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          presets: [
            '@babel/preset-env'
          ]
        }
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif|icon)$/i,
        type: "asset/resource",
        generator: {
          filename: "img/[name].[hash:6][ext]"
        }
      },
      {
        test: /\.html$/,
        //处理html文件的img图片
        loader: 'html-loader',
        options:{
          esModule:false,
          sources:{
            list:[
              {
                tag: "img",
                attribute: "src",
                type: "src",
              },
            ]
          }
        }
      },
      {
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
        ]
      },
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, './src/index.html')
    }),
    new MiniCssExtractPlugin({
      filename:'css/[name].[hash:6].css'
    }),
    new CssMinimizerWebpackPlugin({
      // minimize: true
    }),
    // new CleanWebpackPlugin()
  ],
  devServer: {
    compress: true,
    port: 8000,
    hot: true,
    open: true
  }
}
